var validator;
$(function() {
	validateEditForm();
	searchData();
	$("#submit").click(function(){
		if ($("#keepAccountForm").valid()) {//校验表单
			validator.resetForm();//重置验证
			$.ajax({
				 type : "POST",
				 url : 'newOrUpdateExpenditureDetail.do',
				 data : $('#keepAccountForm').serialize(),//
				 dataType : 'json',
				 success : function(response, ioArgs) {
					 $("#clear").click();
					 hiOverAlert(response.OPERATOR_RESULT);
					 detailList.query({
							click : false
						});
				}
			});
		}
	});
	/**
	 * 一级分类改变，二级分类跟随改变
	 */
	$("#firstClassCategory").change(function(){
		changeSelectValue();
});
	/**
	 * 重置
	 */
	 $("#clear").click(function(){
		 $("#reset").click();
		 validator.resetForm();//重置验证
		 changeSelectValue();
	 });
	 
});

/**
 * 一级菜单变化后的二级菜单更新
 */
function changeSelectValue(){
	var fisetClassCategoryId = $("#firstClassCategory").val();
	$.ajax({
		type : "POST",
		url : 'getSubCategoryByParentId.do',
		data : {'parentId':fisetClassCategoryId},//
		dataType : 'json',
		success : function(response, ioArgs) {
			$('#secondClassCategory option').remove();
			var subCategoryList =response.categoryList;
			if(subCategoryList != null){
				for(var i=0;i<subCategoryList.length;i++){
					var temp = subCategoryList[i];
					var opt =$("<option/>").attr("value",temp["id"]).html(temp["categoryName"]);
					opt.appendTo($('#secondClassCategory'));
				}
			}
		}
	});
}

function searchData() {
	detailList = $('#tableDiv').grid(
		{
			url : "getExpenditureDetail.do",
			root : 'detailList',
			paging:true,
			defaultParams : {
				click : true
			},
			fields : [
					{id : "category", subattr : "categoryName",name : '分类'},
					{id : "merchant",subattr : "merchantName", name : '商店'},
					{id : "amount",name : '金额'},
					{id : "happenedDate",name : '日期',type:'time'},
					{id : "account",subattr : "accountName", name : '账户'},
					{id : "project",subattr : "projectName", name : '项目'},
					{id : "remark",name : '备注'},
					
					{name : "操作",style : {'text-align' : 'center'},
						render : function(data) {
							var box = $("<span/>");
							//编辑
							var edit=$('<a />').attr("href","javascript:void(0)").attr('class','button_a').append($('<span />').html("编辑  ")).bind('click',function(){
								$.ajax({
									type : "POST",
									url : 'getSuperAndSubCategoryBySubId.do',
									data : {'id':data.category.id},//
									dataType : 'json',
									success : function(response, ioArgs) {
										$('#secondClassCategory option').remove();
										var subCategoryList =response.categoryList;
										if(subCategoryList != null){
											for(var i=0;i<subCategoryList.length;i++){
												var temp = subCategoryList[i];
												var opt =$("<option/>").attr("value",temp["id"]).html(temp["categoryName"]);
												opt.appendTo($('#secondClassCategory'));
											}
										}
										$('#detailId').val(data.id);
										$('#firstClassCategory').val(response.parentId);//TODO
										$('#secondClassCategory').val(data.category.id);
										$('#account').val(data.account.id);
										$('#project').val(data.project.id);
										$('#merchant').val(data.merchant.id);
										$('#happenedDate').val(data.happenedDate);
										$('#amount').val(data.amount);
										$('#remark').val(data.remark);
									}
								});
								
								
								
								
							}); 	
							edit.appendTo(box);
								
							//删除
							var deletaObj = $('<a />').attr("href", "#").attr('class', 'button_a').append($('<span />').html("删除  ")).bind(
							'click',function() {
								hiConfirm('确认删除该记录？', '提示', function(flag) {
									if(flag){
										$.ajax({
											type : "POST",//TODO
											url : 'deleteIncomeAndExpenditureDetail.do',
											data : {
												'id' : data.id
											},
											dataType : 'json',
											success : function(response,ioArgs) {
												hiOverAlert(response.OPERATOR_RESULT);
												detailList.query({
													click : false
												});
											}	
										});
								    }
								});
							});

						    deletaObj.appendTo(box);
							return box;

						}
					} ]
		});
}
/**
 * 校验规则
 */
function validateEditForm(){  
	 validator= $("#keepAccountForm").validate({  
		 success:'checked',
		 rules: {
			 'detail.happenedDate': {   
				 required:true
			 },
			 'detail.amount': {
				 required:true,
				 number:true,	    	
				 rangelength:[0,10]
			 }
		 },
		 messages:{			
			 'detail.happenedDate': {   
				 required:"请填写日期！"
			 },
			 'detail.amount': {	
				 required:"请填写金额！",
				 number:"金额只能为数字！",	    	
				 rangelength:"长度在10以内！"
			 }
		 }
	 });
} 